package com.an.blog.web.controller;

import cn.dev33.satoken.annotation.SaIgnore;
import cn.hutool.core.bean.BeanUtil;
import com.an.blog.admin.domain.BlogComment;
import com.an.blog.service.IBlogArticleService;
import com.an.blog.service.IBlogCommentService;
import com.an.blog.web.domain.bo.BlogArticleWebBo;
import com.an.blog.web.domain.bo.BlogCommentWebBo;
import com.an.blog.web.domain.vo.ArchiveGroupYearWebVo;
import com.an.blog.web.domain.vo.BlogArticleDetailWebVo;
import com.an.blog.web.domain.vo.BlogArticleWebVo;
import com.an.common.annotation.Log;
import com.an.common.constant.BlogConstants;
import com.an.common.core.controller.BaseController;
import com.an.common.core.domain.PageQuery;
import com.an.common.core.domain.R;
import com.an.common.core.page.TableDataInfo;
import com.an.common.enums.BusinessType;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 博客文章web控制器
 *
 * @author: Ann
 * @createDate: 2023/05/26 16:31:20
 * @version: 1.0.0
 */
@RestController
@RequestMapping("/blog-web/article")
@RequiredArgsConstructor
public class BlogArticleWebController extends BaseController {
    private final IBlogArticleService articleService;
    private final IBlogCommentService commentService;

    /**
     * 查询文章列表
     *
     * @param articleWebBo
     * @param pageQuery
     * @return {@link TableDataInfo }<{@link BlogArticleWebVo }>
     * @author: Ann
     * @createDate: 2023/09/01 14:43:38
     * @version: 1.0.0
     */
    @SaIgnore
    @GetMapping("/list")
    public TableDataInfo<BlogArticleWebVo> queryWebArticleList(BlogArticleWebBo articleWebBo, PageQuery pageQuery) {
        return articleService.queryWebPageList(articleWebBo, pageQuery);
    }


    /**
     * 查询网站文章id
     *
     * @param articleId 文章id
     * @return {@link TableDataInfo }<{@link BlogArticleWebVo }>
     * @author: Ann
     * @createDate: 2023/06/07 18:39:48
     * @version: 1.0.0
     */
    @SaIgnore
    @GetMapping("/{articleId}")
    public R<BlogArticleDetailWebVo> queryWebArticleById(@PathVariable Long articleId) {
        return R.ok(articleService.queryWebArticleById(articleId));
    }


    /**
     * 查看文章
     *
     * @param articleId 文章编号
     * @return {@link R }<{@link Void }>
     * @author Ann
     * @date 2024/06/16
     */
    @SaIgnore
    @PutMapping("/view/{articleId}")
    public R<Void> viewArticle(@PathVariable Long articleId) {
        return toAjax(articleService.viewArticle(articleId));
    }

    /**
     * 喜欢文章
     *
     * @param articleId 文章id
     * @return {@link R }<{@link Void }>
     * @author: Ann
     * @createDate: 2023/06/07 14:37:42
     * @version: 1.0.0
     */
    @PutMapping("/like/{articleId}")
    @Log(title = "文章点赞", businessType = BusinessType.UPDATE)
    public R<Void> likeArticle(@PathVariable Long articleId) {
        return toAjax(articleService.likeArticle(articleId));
    }

    /**
     * 评论文章
     *
     * @param commentWebBo 评论网络业务对象
     * @return {@link R }<{@link Void }>
     * @author: Ann
     * @createDate: 2023/06/08 15:27:56
     * @version: 1.0.0
     */
    @PostMapping("/comment")
    @Log(title = "文章评论", businessType = BusinessType.INSERT)
    public R<Void> commentArticle(@RequestBody @Validated BlogCommentWebBo commentWebBo) {
        BlogComment blogComment = BeanUtil.toBean(commentWebBo, BlogComment.class);
        //评论文章
        blogComment.setTopic(BlogConstants.COMMENT_ARTICLE_TOPIC);
        return toAjax(commentService.comment(blogComment));
    }

    /**
     * 查询文章归档
     *
     * @return {@link TableDataInfo }<{@link ArchiveGroupYearWebVo }>
     * @author: Ann
     * @createDate: 2023/11/30 10:50:57
     * @version: 1.0.0
     */
    @SaIgnore
    @GetMapping("/archiveList")
    public R<List<ArchiveGroupYearWebVo>> queryWebArchiveList() {
        return R.ok(articleService.queryWebArchiveList());
    }

    @SaIgnore
    @GetMapping("/aboutMe")
    public R<BlogArticleDetailWebVo> queryAboutMeArticle() {
        return R.ok(articleService.queryAboutMeArticle());
    }

}
